<template>
	<view class="app-container">
		<view class="content" :class="isKey ? 'active' : ''">
			<view class="item" v-for="(item,index) in talkList" :key="index">
				<view class="item-left" v-if="!item.me">
					<image class="avatar" :src="friendInfo.avatar ? friendInfo.avatar:defaultAvatar">
						<view class="triangle"></view>
						<view class="msg-box">
							{{item.content}}
						</view>
				</view>
				<view class="item-right" v-else>
					<view class="triangle"></view>
					<view class="msg-box">
						{{item.content}}
					</view>
					<image class="avatar" :src="userInfo.avatar ? userInfo.avatar:defaultAvatar">
				</view>
			</view>
			<view style="wdith: 100%;height: 50px;"></view>
		</view>
		<view class="footer">
			<view class="footer-left">
				<image src="../../static/images/spack.png"></image>
			</view>
			<view class="footer-middle">
				<input type="text" placeholder="请输入内容" v-model="inputText" @confirm="handleSendMsg" @keyboardheightchange="changeKeyBoard" />
			</view>

			<view class="footer-right">
				<image src="../../static/images/add.png" mode=""></image>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		sendObj
	} from '../../utils/websocket.js'
	import {
		getUser
	} from '../../utils/auth.js'
	export default {

		data() {
			return {
				defaultAvatar: require('../../static/images/avatar.png'),
				nickname: '',
				friendId: '',
				friendInfo: {},
				userInfo: {},
				inputText: '',
				styles: {},
				scrollTop: 0,
				talkList: [],
				isKey:false
			}
		},
		onLoad(options) {

			this.getUserInfo()
			this.nickname = options.nickname
			this.friendId = options.friendId
			uni.setNavigationBarTitle({
				title: this.nickname
			});
			this.getFriendInfo(this.friendId)
			this.talkList = uni.getStorageSync(`talk_${this.userInfo.userInfo.userId}_${this.friendId}`) || []
			uni.$on('sendMsg', (data) => {
				console.log('消息收到了', data);
				this.talkList.push({
					content: data.content,
					me: false
				})

			})
		},
		onBackPress() {
			uni.setStorageSync(`talk_${this.userInfo.userInfo.userId}_${this.friendId}`, this.talkList)
		},
		onHide() {
			uni.setStorageSync(`talk_${this.userInfo.userInfo.userId}_${this.friendId}`, this.talkList)
		},
		methods: {
			getUserInfo() {
				this.userInfo = getUser()
			},

			getFriendInfo(id) {
				this.$http('/admin/sys/user/info/' + id).then(res => {
					this.friendInfo = res.data.data
				})
			},
			handleSendMsg() {
				sendObj({
					did: uni.getSystemInfoSync().deviceId,
					type: 3,
					fid: this.userInfo.userInfo.userId,
					tid: this.friendId,
					content: this.inputText
				})
				this.talkList.push({
					content: this.inputText,
					me: true
				})
				this.inputText = ''
				this.$nextTick(() => {
					uni.pageScrollTo({
						scrollTop: this.talkList.length * 50,
						duration: 1
					})
				})
				
			},

			changeKeyBoard(e) {
				console.log(e, 'eeeee');
				if(e.detail.height > 0){
					this.isKey = true
					this.$nextTick(() => {
						uni.pageScrollTo({
							scrollTop: this.talkList.length * 50,
							duration: 1
						})
					})
				}else {
					this.isKey = false
				}
			}

		}

	}
</script>

<style>

	.content {
		background-color: #f8f8f8;
		padding-top: 2px;
	}
	
	.active{
		background-color: #f8f8f8;
		padding-top: 340px;
	}

	.item-left {
		display: flex;
		margin: 10px 0;
		position: relative;
		justify-content: flex-start;
	}

	.item-left .avatar {
		width: 40px;
		height: 40px;
		background-color: #fff;
		border-radius: 5px;
		margin-left: 10px;
	}

	.item-left .msg-box {
		min-width: 30px;
		max-width: 420rpx;
		line-height: 40px;
		background-color: #fff;
		margin: 0 15px;
		border-radius: 5px;
		padding: 0 12px;
		box-sizing: border-box;
		z-index: 99;
	}

	.item-left .triangle {
		display: block;
		position: absolute;
		top: 12px;
		left: 60px;
		width: 15px;
		height: 15px;
		border-radius: 3px;
		background-color: #fff;
		transform: rotate(45deg);
		z-index: 1;
	}

	.item-right {
		display: flex;
		justify-content: flex-end;
		margin: 10px 0;
		position: relative;
	}

	.item-right .avatar {
		width: 40px;
		height: 40px;
		margin-right: 10px;
		background-color: #fff;
		border-radius: 5px;
	}

	.item-right .msg-box {
		min-width: 30px;
		max-width: 420rpx;
		line-height: 40px;
		background-color: #007AFF;
		margin: 0 15px;
		border-radius: 5px;
		box-sizing: border-box;
		padding: 0 12px;
		z-index: 99;
		color: #fff;
	}

	.item-right .triangle {
		display: block;
		position: absolute;
		top: 12px;
		right: 60px;
		width: 15px;
		height: 15px;
		border-radius: 3px;
		background-color: #007AFF;
		transform: rotate(45deg);
		z-index: 1;
	}

	.footer {
		display: flex;
		justify-content: space-between;
		align-items: center;
		height: 50px;
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		box-sizing: border-box;
		border-top: 1px solid #ccc;
		background-color: #f8f8f8;
		z-index: 99;
	}

	.footer-left {
		width: 34px;
		height: 34px;
		margin: 0 10px;
	}

	.footer-left image {
		width: 100%;
		height: 100%;
	}

	.footer-middle {
		flex: 1;
		background-color: #fff;
		height: 38px;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.footer-middle input {
		box-sizing: border-box;
		width: 100%;
		height: 50px;
		padding: 5px 10px;
	}

	.footer-right {
		width: 34px;
		height: 34px;
		margin: 0 10px;
	}
	
	.footer-right image {
		width: 100%;
		height: 100%;
	}
</style>
